﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ViewBoxScale
{
    public partial class MainWindow
    {
        private readonly List<string> _files;
        private bool _startMove;
        private int _picIndex;
        private Point _start;

        public MainWindow()
        {
            InitializeComponent();
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Pics");
            //files = Directory.GetFiles(path,"*.jpg").ToList();
            _files = Directory.GetFiles(path).ToList();
            ButtonBase_OnClick(null, null);
        }

        private void ChangSize(bool big, FrameworkElement ctrl, double centerX, double centerY)
        {
            var m = ((MatrixTransform) ctrl.RenderTransform).Matrix;
            if (big)
            {
                m.ScaleAtPrepend(1.1, 1.1, centerX, centerY);
            }
            else
            {
                m.ScaleAtPrepend(1/1.1, 1/1.1, centerX, centerY);
            }
            ctrl.RenderTransform = new MatrixTransform(m);
        }

        private void Box_OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var centerPt = e.GetPosition(Box);
            ChangSize(e.Delta > 0, Box, centerPt.X, centerPt.Y);
            e.Handled = true; //避免缩放时同时影响滚动条;
        }

        private void Box_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Box.IsMouseCaptured) return;
            Box.CaptureMouse();
            _start = e.MouseDevice.GetPosition(Sv);
            e.Handled = true;
            _startMove = true;
        }

        private void Box_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Box.ReleaseMouseCapture();
            _startMove = false;
        }

        private void Box_OnPreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (!Box.IsMouseCaptured || !_startMove) return;
            var point = e.MouseDevice.GetPosition(Sv);
            var m = Box.RenderTransform.Value;
            //计算x偏移量 
            m.OffsetX = m.OffsetX + point.X - _start.X;
            //计算y偏移量
            m.OffsetY = m.OffsetY + point.Y - _start.Y;
            Box.RenderTransform = new MatrixTransform(m);
            //给鼠标起点和图片临时位置重新赋值
            _start = point;
        }

        /// <summary>
        /// 切换图片
        /// </summary>
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            if (_picIndex == _files.Count)
            {
                _picIndex = 0;
            }
            Txt.Text = _picIndex.ToString();
            var bmp = new BitmapImage(new Uri(_files[_picIndex]));
            Img.Source = bmp;
            _picIndex++; 
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Box.RenderTransform=new MatrixTransform(Matrix.Identity);
            InfoGrid.Visibility = Visibility.Visible;
        }
    }
}